﻿Ext.define('I2R.controller.UserController', {
    extend: 'I2R.controller.BaseController',
    views: [
        'userformpanel',
        'configusers'
    ],
    stores: ['users','roles'],
    refs: [
        { ref: 'userformpanel', selector: 'i2ruserformpanel' },
        { ref: 'usersgrid', selector: '#usersgrid' }
    ],
    init: function () {

        var me = this;

        this.control({

            '#my-user-profile button[action=save-profile]': {
                click: function (btn, e) {

                    var frm = this.getUserformpanel().getForm();
                    var win = Ext.getCmp('my-user-profile');

                    if (frm.isValid()) {
                        frm.submit({
                            method: 'POST',
                            url: '../User/Settings',
                            success: function (form, action) {
                                Ext.Msg.alert('Success', 'Successfully saved!');
                                win.close();
                            },
                            failure: function (form, action) {
                                Ext.Msg.alert('Failed', action.result.data.errorMessage);
                            }
                        });
                    } else {

                    }

                }
            },

            '#usersgrid': {
                itemdblclick: function (dv, record, item, index, e) {
                    
                    var id = record.get('Id');

                    var win = new Ext.Window({
                        title: 'New user',
                        id: 'my-user-profile',
                        iconCls: 'icon_user',
                        items: [{ xtype: 'i2ruserformpanel', skipLoad: false, loadId: id, userEdit: true}],
                        height: 500,
                        width: 800,
                        layout: 'fit',
                        buttons: [
                            { text: 'Save', iconCls: 'icon_disk', action: 'save-profile' },
                            { text: 'Cancel', iconCls: 'icon_cancel',
                                handler: function () {
                                    Ext.getCmp('my-user-profile').close();
                                }
                            }
                        ]
                    });

                    win.on('close', function () {
                        me.getUsersgrid().store.load();
                    });

                    win.show();

                }
            },

            '#usersgrid button[action=adduser]': {
                click: function (btn, e) {
                    // TODO: Create a window for userdetails like  my-user-profile.

                    var win = new Ext.Window({
                        title: 'New user',
                        id: 'my-user-profile',
                        iconCls: 'icon_user',
                        items: [{ xtype: 'i2ruserformpanel', skipLoad: true, userEdit: true}],
                        height: 500,
                        width: 800,
                        layout: 'fit',
                        buttons: [
                            { text: 'Save', iconCls: 'icon_disk', action: 'save-profile' },
                            { text: 'Cancel', iconCls: 'icon_cancel',
                                handler: function () {
                                    Ext.getCmp('my-user-profile').close();
                                }
                            }
                        ]
                    });

                    win.on('close', function () {
                        me.getUsersgrid().store.load();
                    });

                    win.show();
                }
            }

        });

    }
});